package com.jojo.android.zxlib.tool;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.Where;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019-1-24.
 */

public class ZxDBHelper extends OrmLiteSqliteOpenHelper {
    private static ZxDBHelper dbHelper;
    public static String DBName = null;
    public static int DBVersion = 1;
    private static Context context;

    private ZxDBHelper(Context context) {
        super(context, DBName, null, DBVersion);
    }

    public static void init(Context context) {
        init(context, null);
    }

    public static void init(Context context, String dbName) {
        ZxDBHelper.context = context;
        if (dbName == null || dbName.equals("")) {
            DBName = "default_db";
        } else {
            DBName = dbName;
        }
    }

    /**
     * 获取单例
     *
     * @return
     */
    public static synchronized ZxDBHelper getInstance() {
        if (context == null) {
            throw new RuntimeException("Context == null !!!  请检查是否初始化DBhelper!  ");
        }
        if (dbHelper == null) {
            dbHelper = new ZxDBHelper(context);
        }
        return dbHelper;
    }

    public static synchronized void DestroyInstance() {
        dbHelper = null;
    }


    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        Log.e("DBHelper", "数据库被创建");
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {

    }

    /**
     * 创建表
     *
     * @param dataClass
     * @return
     */
    public synchronized int createTable(Class dataClass) {
        if (dataClass == null) return -1;
        try {
            return TableUtils.createTable(getConnectionSource(), dataClass);
        } catch (SQLException e) {
            ZxLogTool.err(e);
        }
        return -1;
    }

    /**
     * 当表不存在时创建
     *
     * @param dataClass
     * @return
     */
    public synchronized int createTableIfNotExists(Class dataClass) {
        if (dataClass == null) return -1;
        try {
            return TableUtils.createTableIfNotExists(getConnectionSource(), dataClass);
        } catch (SQLException e) {
            ZxLogTool.err(e);
        }
        return -1;
    }

    public int isExistTable(Class table) {
        try {
            return getDao(table).isTableExists() == true ? 1 : 0;
        } catch (SQLException e) {
            ZxLogTool.err(e);
            return -1;
        }
    }

    /**
     * 增
     * 向表中插入数据
     *
     * @param classData
     * @param data
     * @param <T>
     * @return
     */
    public synchronized <T> int insert(Class<T> classData, T data) {
        if (classData == null || data == null) return -1;
        try {

            return getDao(classData).create(data);
        } catch (SQLException e) {
            ZxLogTool.err(e);
        }
        return -1;
    }

    public synchronized <T> int insert(Class<T> classData, Collection<T> data) {
        if (classData == null || data == null) return -1;
        try {
            return getDao(classData).create(data);
        } catch (SQLException e) {
            ZxLogTool.err(e);
        }
        return -1;
    }

    /**
     * 查
     *
     * @param classData 要查询的类型的Class对象
     * @param fields    这个是一个MAP集合，属于查询条件，String表示字段名，object表示值。
     * @param <T>
     * @return
     */
    public synchronized <T> List<T> queryForFieldValues(Class<T> classData, Map<String, Object> fields) {
        try {
            if (fields == null) {
                return getDao(classData).queryForAll();
            }
            return getDao(classData).queryForFieldValues(fields);
        } catch (SQLException e) {
            ZxLogTool.err(e);
        }
        return null;
    }

    public synchronized <T> List<T> queryAll(Class<T> classData) {
        try {
            return getDao(classData).queryForAll();
        } catch (SQLException e) {
            ZxLogTool.err(e);
        }
        return null;
    }

    /**
     * 根据字段的值来查询
     *
     * @param classData
     * @param name
     * @param v
     * @param <T>
     * @return
     */
    public synchronized <T> List<T> queryForEq(Class<T> classData, String name, Object v) {
        try {
            return getDao(classData).queryForEq(name, v);
        } catch (SQLException e) {
            ZxLogTool.err(e);
        }
        return null;
    }


    /**
     * 改
     *
     * @param classData
     * @param data
     * @param <T>
     * @return
     */
    public synchronized <T> int update(Class<T> classData, T data) {
        try {
            getDao(classData).update(data);
        } catch (SQLException e) {
            ZxLogTool.err(e);
        }
        return -1;
    }

    /**
     * 删
     *
     * @param classData
     * @param data
     * @param <T>
     * @return
     */
    public synchronized <T> int delete(Class<T> classData, T data) {
        try {
            return getDao(classData).delete(data);
        } catch (SQLException e) {
            ZxLogTool.err(e);
        }
        return -1;
    }

    /**
     * 删除数据集
     *
     * @param classData
     * @param datas
     * @param <T>
     * @return
     */
    public synchronized <T> int deleteAll(Class<T> classData, List<T> datas) {
        try {

            return getDao(classData).delete(datas);
        } catch (Exception e) {
            ZxLogTool.err(e);
        }
        return -1;
    }

    /**
     * 按条件删除
     *
     * @param classData
     * @param k
     * @param v
     * @param <T>
     * @return
     */
    public synchronized <T> int deleteForEq(Class<T> classData, String k, Object v) {
        try {
            DeleteBuilder deleteBuilder = getDao(classData).deleteBuilder();
            Where where = deleteBuilder.where().eq(k, v);
            deleteBuilder.setWhere(where);
            return deleteBuilder.delete();
        } catch (Exception e) {

        }
        return -1;
    }

}
